/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: UserControl
 * Author:   臧浩鹏
 * Date:     2018/7/18 9:08
 * Description: Control类进行student的登录验证
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.play.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.play.common.FileUtil;
import com.play.model.Article;
import com.play.model.ArticleDeatil;
import com.play.service.IArticleDetailService;
import com.play.service.IArticleService;
import com.play.service.IRegService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * 〈Control类进行student的登录验证〉
 *
 * @author 臧浩鹏
 * @create 2018/7/18
 * @since 1.0.0
 */
@Slf4j
@Controller
@EnableAutoConfiguration
public class UserControl {
    @Autowired
    private IRegService regService;
    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private IArticleDetailService iArticleDetailService;

    @RequestMapping("/")
    String home(){
        return "index";
    }

    @RequestMapping("/show")
    String redir(){
        return "show";
    }

    @RequestMapping("/select")
    @ResponseBody
    Article show(){
        int aid = 18;
        Article article = iArticleService.selectByAId(aid);
        ArticleDeatil articleDeatil = iArticleDetailService.selectByAdid(aid);
        article.setDetail(articleDeatil.getAddetail());
        log.info("准备返回页面！");
        return article;
    }


    @RequestMapping("/getWrite")
    @ResponseBody
    String mark(@RequestBody() Article article){
        System.out.println(article);
        Integer aid = iArticleService.insertArticleByAIdAndUId(article, 1);
        System.out.println("文章主键: "+aid);
        Integer res = iArticleDetailService.insertArticleDetailByAId(aid, article.getDetail());
        if(res == 1){
            System.out.println("恭喜！ 插入成功！");
        }else {
            System.out.println("失败！请检查！");
        }
        return "666";
    }
    @RequestMapping("/imgUp")
    @ResponseBody
    public String hello(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "editormd-image-file", required = true) MultipartFile[] attach){
        try {
            request.setCharacterEncoding( "utf-8" );
            response.setHeader( "Content-Type" , "text/html" );
            /*String rootPath = request.getSession().getServletContext().getRealPath("/resources/upload/");*/
            String path = FileUtil.saveMultiFile("D:/upload", attach);
            /**
             * 文件路径不存在则需要创建文件路径
             */
            /*File filePath=new File(rootPath);
            if(!filePath.exists()){
                filePath.mkdirs();
                log.info("file路径："+filePath);
            }*/



            /*//最终文件名
            File realFile=new File(rootPath+ File.separator+attach.getOriginalFilename());
            FileUtils.copyInputStreamToFile(attach.getInputStream(), realFile);*/
            ObjectMapper mapper = new ObjectMapper();
            HashMap<String, Object> map = new HashMap<>();
            map.put("success",1);
            map.put("message","上传成功");
            map.put("url",path);
            /*log.info(filePath+"\\"+ attach[0].getOriginalFilename());*/
            log.info(path);
            String s = mapper.writeValueAsString(map);
            return s;
            //下面response返回的json格式是editor.md所限制的，规范输出就OK
            //response.getWriter().write( "{\"success\": 1, \"message\":\"上传成功\",\"url\":\"/resources/upload/" + attach.getOriginalFilename() + "\"}" );
        } catch (Exception e) {
            try {
                response.getWriter().write( "{\"success\":0}" );
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return response.toString();
    }

    @RequestMapping("/reg")
    @ResponseBody
    Boolean reg(@RequestParam("loginID") String uId,@RequestParam("loginName") String uName){
        System.out.println(uId+":"+uName);
        if (uId.equals("")&&uName.equals("")){
            System.out.println("无信息");
            return false;
        }
        boolean res = regService.regUser(uId, uName);
        return res;
    }
}
